﻿using Bussiness;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using CCWin;

namespace Scada
{
    public partial class FrmDevAdd :Skin_Mac
    {
        public string _DevGuid;
        BLL _BLL = new BLL();
        public event ONBindDeviceListHandler ONBindDeviceList;
        public FrmDevAdd()
        {
            InitializeComponent();
        }

        private void FrmDevAdd_Load(object sender, EventArgs e)
        {
            cbx设备类型.FieldList = _BLL._DAL.GetDeviceTypeFieldList();
            cbx设备类型.Bind();

            Device device = _BLL._DAL.GetDevice(_DevGuid);
            if (device != null)
            {
                this.txt设备编号.Text = device.DevCode;
                this.txt设备名称.Text = device.DevName;
                this.txtIP.Text = device.IP;
                this.txt端口.Text = device.Port.ToString();
                this.cbx设备类型.SelectedValue = device.DevTypeGuid;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string devCode = this.txt设备编号.Text.Trim();
            string devName = this.txt设备名称.Text.Trim();
            string ip = this.txtIP.Text.Trim();
            string port = this.txt端口.Text.Trim();
            string devTypeGuid = this.cbx设备类型.SelectedValue.ToString();
            if (string.IsNullOrEmpty(devCode))
            {
                MessageBox.Show("设备编号空！");
                return;
            }

            if (string.IsNullOrEmpty(devName))
            {
                MessageBox.Show("设备名称空！");
                return;
            }

            Device device = new Device();
            device.Guid = string.IsNullOrEmpty(_DevGuid) ? Guid.NewGuid().ToString() : _DevGuid;
            device.DevCode = devCode;
            device.DevName = devName;
            device.IP = ip;
            device.Port = port.ToInt();
            device.DevTypeGuid = devTypeGuid;
            Result result = _BLL._DAL.SaveDevice(device,string.IsNullOrEmpty(_DevGuid) ?true:false);
            if (result.Success)
            {
                MessageBox.Show("成功！");
                ONBindDeviceList();
                this.Close();
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
